package com.cardbaobao.cardbabyclient.ui.pop;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.activity.web.EventWebViewActivity;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.model.Banner;
import com.cardbaobao.cardbabyclient.model.dao.LauncherAdClickedDao;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.utils.SharedPreferencesUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.stx.xhb.xbanner.XBanner;

import java.util.List;

/**
 * Created by Jiangzx on 2017/7/12 10:20.
 * 首页广告展示popupWindow，可左右滑动
 */

public class LauncherAdPop extends PopupWindow {
    private static final int MARGIN=70;
    private static final float RATIO=1.3f;
    private XBanner xBanner;
    public LauncherAdPop(final Context context, final List<Banner.CarouselFigureListBean> data){
        int width= ScreenUtil.getScreenWidth()-MARGIN*2;
        int height= (int) (width*RATIO);
        FrameLayout container=new FrameLayout(context);
        FrameLayout.LayoutParams params=
                new FrameLayout.LayoutParams(width,height);
        params.gravity=Gravity.CENTER;
        container.setLayoutParams(params);
        //关闭按钮
        ImageButton ibtClose=new ImageButton(context);
        FrameLayout.LayoutParams closeParams=
                new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                        FrameLayout.LayoutParams.WRAP_CONTENT);
        Drawable closeDrawable= ContextCompat.getDrawable(context,R.mipmap.icon_close_white);
        closeParams.gravity= Gravity.END;
        closeParams.topMargin= (int) ((ScreenUtil.getScreenHeight()-height)/2.0f)-
                closeDrawable.getMinimumHeight()*2;
        closeParams.rightMargin=MARGIN;
        ibtClose.setLayoutParams(closeParams);
        ibtClose.setImageDrawable(closeDrawable);
        ibtClose.setBackgroundResource(R.color.transparent);
        ibtClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferencesUtil.CbbInfo.setLatestCloseAdPopTime(System.currentTimeMillis());
                dismiss();
            }
        });

        xBanner=new XBanner(context);
        xBanner.setLayoutParams(params);
        xBanner.setmAutoPalyTime(Constant.BANNER_INTERVAL_TIME);
        xBanner.setPoinstPosition(XBanner.RIGHT);
        xBanner.setPointsIsVisible(true);
        //设置完数据后会自动开始轮播，不需要手动设置，否则会一次内阁滚动2个
        xBanner.setData(data,null);
        xBanner.setmAdapter(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+data.get(position).getImage(),
                        (ImageView) view,
                        R.mipmap.img_banner_default_big);
            }
        });
        xBanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, int position) {
                String url=data.get(position).getUrl();
                if(!TextUtils.isEmpty(url)){
                    dismiss();
                    new LauncherAdClickedDao().inSertClickedAdUrl(url);
                    Intent intent=new Intent(context, EventWebViewActivity.class);
                    intent.putExtra(EventWebViewActivity.WEB_TITLE_S,
                            data.get(position).getTopical());
                    intent.putExtra(EventWebViewActivity.WEB_URL_S,
                            url);
                    intent.putExtra(EventWebViewActivity.WEB_TYPE_I,40);
                    context.startActivity(intent);
                }
            }
        });
        container.addView(xBanner);
        container.addView(ibtClose);

        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        setContentView(container);
        setBackgroundDrawable(new ColorDrawable(0xCC000000));
        setFocusable(true);
        setTouchable(true);
        setClippingEnabled(false);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    public void stopXBannerLoop(){
        if(xBanner!=null){
            xBanner.stopAutoPlay();
        }
    }
}
